package cn.mw.cmdb.enums;

import cn.hutool.json.JSONArray;
import com.alibaba.fastjson.JSONObject;

public enum DateUnitEnum {

    MINUTE("分钟", "minute") {
        @Override
        public Boolean verify(int value) {
            return value >= 0 && value <= 59;
        }
    },
    HOUR("小时", "hour") {
        @Override
        public Boolean verify(int value) {
            return value >= 0 && value <= 23;
        }
    },
    DAY("天", "day") {
        @Override
        public Boolean verify(int value) {
            return value >= 1 && value <= 31;
        }
    },
    WEEK("周", "week") {
        @Override
        public Boolean verify(int value) {
            return value >= 1 && value <= 7;
        }
    },
    MONTH("月", "month") {
        @Override
        public Boolean verify(int value) {
            return value >= 1 && value <= 12;
        }
    },
    YEAR("年", "year") {
        @Override
        public Boolean verify(int value) {
            return value > 0;
        }
    };

    private final String chineseName;
    private final String englishName;

    private static final String KEY = "dateUnit";

    DateUnitEnum(String chineseName, String englishName) {
        this.chineseName = chineseName;
        this.englishName = englishName;
    }

    public String getChineseName() {
        return chineseName;
    }

    public String getEnglishName() {
        return englishName;
    }

    public abstract Boolean verify(int value);


    public static DateUnitEnum getDateUnitEnumByEName(String englishName) {
        for (DateUnitEnum value : DateUnitEnum.values()) {
            if (value.getEnglishName().equals(englishName)) {
                return value;
            }
        }
        return null;
    }

    public static JSONObject getAll() {
        JSONObject object = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        for (DateUnitEnum value : DateUnitEnum.values()) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("chineseName", value.getChineseName());
            jsonObject.put("englishName", value.getEnglishName());
            jsonArray.add(jsonObject);
        }
        object.put(KEY, jsonArray);
        return object;
    }
}

